package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WorkOrderMapper;
import com.ruoyi.system.domain.WorkOrder;
import com.ruoyi.system.service.IWorkOrderService;

/**
 * 工单管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-06-02
 */
@Service
public class WorkOrderServiceImpl implements IWorkOrderService 
{
    public static final Integer STATUS_OK=1;
    public static final Integer STATUS_NO=0;
    @Autowired
    private WorkOrderMapper workOrderMapper;

    /**
     * 查询工单管理
     * 
     * @param id 工单管理主键
     * @return 工单管理
     */
    @Override
    public WorkOrder selectWorkOrderById(Long id)
    {
        return workOrderMapper.selectWorkOrderById(id);
    }

    /**
     * 查询工单管理列表
     * 
     * @param workOrder 工单管理
     * @return 工单管理
     */
    @Override
    public List<WorkOrder> selectWorkOrderList(WorkOrder workOrder)
    {

        return workOrderMapper.selectWorkOrderList(workOrder);
    }

    /**
     * 新增工单管理
     * 
     * @param workOrder 工单管理
     * @return 结果
     */
    @Override
    public int insertWorkOrder(WorkOrder workOrder)
    {
            //判断是否有回复
        if (workOrder.getReplyContent() !=null){
            WorkOrder workOrders=new WorkOrder();
            BeanUtils.copyProperties(workOrder,workOrders);
            //有 修改 状态
            workOrders.setIsReplied(STATUS_OK);
            //添加
            return workOrderMapper.insertWorkOrder(workOrders);
        }


        return workOrderMapper.insertWorkOrder(workOrder);
    }

    /**
     * 修改工单管理
     * 
     * @param workOrder 工单管理
     * @return 结果
     */
    @Override
    public int updateWorkOrder(WorkOrder workOrder)
    {

        //判断是否有回复
        if (workOrder.getReplyContent() ==null){
            WorkOrder workOrders=new WorkOrder();
            BeanUtils.copyProperties(workOrder,workOrders);
            //有 修改 状态
            workOrders.setIsReplied(STATUS_NO);
            //添加
            return workOrderMapper.updateWorkOrder(workOrders);
        }
        return workOrderMapper.updateWorkOrder(workOrder);
    }

    /**
     * 批量删除工单管理
     *
     * @param ids 需要删除的工单管理主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderByIds(Long[] ids)
    {
        for (Long id : ids) {
            WorkOrder workOrder = workOrderMapper.selectWorkOrderById(id);
            Integer isReplied = workOrder.getIsReplied();
            if (isReplied == STATUS_OK ){
                throw new ServiceException("已回复不允许删除");
            }

        }
        return workOrderMapper.deleteWorkOrderByIds(ids);
    }

    /**
     * 删除工单管理信息
     * 
     * @param id 工单管理主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderById(Long id)
    {
        WorkOrder workOrder = workOrderMapper.selectWorkOrderById(id);

        Integer isReplied = workOrder.getIsReplied();
        if (isReplied == STATUS_OK ){
           throw new RuntimeException("已回复不允许删除");
        }
        return workOrderMapper.deleteWorkOrderById(id);
    }

    /**
     * 派单修改
     * @param id
     * @param name
     */
    @Override
    public void updateWorkOrderById(Long id, String name) {
        //查出要修改的工单
        WorkOrder workOrder = workOrderMapper.selectWorkOrderById(id);
        //添加上派单人
        workOrder.setAssignee(name);
        //修改派单状态
        if (!workOrder.getIsAssigned().equals(STATUS_NO)){
            //更新时间
            workOrder.setUpdatedAt(new Date());
            //修改
            workOrderMapper.updateWorkOrder(workOrder);
        }

        workOrder.setIsAssigned(STATUS_OK);
        //更新时间
        workOrder.setUpdatedAt(new Date());
        //修改
        workOrderMapper.updateWorkOrder(workOrder);
    }

    /**
     * 回复修改
     * @param id
     * @param name
     */
    @Override
    public void updateWorkOrderByIds(Long id, String name) {
        //查出要修改的工单
        WorkOrder workOrder = workOrderMapper.selectWorkOrderById(id);
        //添加上回复内容
        workOrder.setReplyContent(name);
        //修改派单状态
        if (!workOrder.getIsReplied().equals(STATUS_NO)){
            //更新时间
            workOrder.setUpdatedAt(new Date());
            //修改
            workOrderMapper.updateWorkOrder(workOrder);
        }

        workOrder.setIsReplied(STATUS_OK);
        //更新时间
        workOrder.setUpdatedAt(new Date());
        //修改
        workOrderMapper.updateWorkOrder(workOrder);
    }
}
